package 二叉树;


public class q108有序数组转换为平衡搜索树 {
    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public TreeNode sortedArrayToBST(int[] nums) {
        return build(nums,0,nums.length-1);
    }
    TreeNode build(int[] nums,int left,int right){
        if(left>right) return null;
        // 选择中间节点作为根节点
        int mid = left + (right - left)/2;
        TreeNode node = new TreeNode(nums[mid]);

        node.left = build(nums,left,mid-1);
        node.right = build(nums,mid+1,right);
        return node;

    }
}
